package com.example.healkep;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class WeightInput extends AppCompatActivity {

    private int id;
    private int preg_week;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        final Intent intent = getIntent();
        id = intent.getIntExtra("id", -1);
        preg_week = intent.getIntExtra("preg_week", 1);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.weight_input_layout);

        final EditText weight = (EditText) findViewById(R.id.weight_value);
        final EditText week = (EditText) findViewById(R.id.week) ;

        week.setText(String.valueOf(preg_week));

        final Button button = findViewById(R.id.send);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            MediaType JSON = MediaType.parse("application/json");
                            JSONObject json = new JSONObject();
                            try {
                                json.put("id", id);
                                json.put("weight", weight.getText());
                                json.put("week", week.getText());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            OkHttpClient client = new OkHttpClient();  //创建http客户端
                            RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                            Request request = new Request.Builder()
                                    .url(LoginActivity.url+"/weight/add")
                                    .post(requestBody)
                                    .build();    //创造http请求
                            Response response = client.newCall(request).execute();  //执行发送的指令
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(WeightInput.this, "上传成功！", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(WeightInput.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();
            }
        });

    }
}
